
capture log close
log using $log/EDS_03_main_results, text replace

clear
clear matrix
clear mata
set mem 2g
set matsize 800
set more off
set maxvar 20000
set trace off
set varabbrev off

	 
	 #d;
	  global cov "
	 _Isu_schule_1 _Isu_schule_2 _Isu_schule_3 
	_Isu_ausbila1 _Isu_ausbila2
	_Isu_kat_al_2 _Isu_kat_al_3 _Isu_kat_al_4 
	_Ichild_1 _Ichild_2
	_Isu_eintri_1 _Isu_eintri_2 _Isu_eintri_3 _Isu_eintri_4 _Isu_eintri_5
	_Isu_eintri_6 _Isu_eintri_7 _Isu_eintri_8 _Isu_eintri_9 _Isu_eintri_10 _Isu_eintri_11
	_Ibefstat_1 _Ibefstat_2 _Ibefstat_3 _Ibefstat_4
	_Idauloint__8 _Idauloint__9 _Idauloint__10 _Idauloint__11 _Idauloint__12 
	_Idauloint__13 _Idauloint__14
	female east_germany
    german migration_background 
    married  
	benefit_status benefit_level log_benefit_level
    lifetime_ue_age lifetime_empl_age last_wage last_wage_mis 
    region_ba_aloq_zivall gdp_real_pc region_ba_vacancies 
	sector_manufacturing sector_service  log_mobirate_avg5
	openness_st conscient_st extraversion_st neuroticism_st loc_st
	write_ger speak_ger write_eng speak_eng
	homeowner   
	   ";
	
	   
	   
		#d;
		global outc"
		   search_dist50 
	       applications_total
		   applications_distant
		   applications_local
		   log_distance_km 
		   search_channel_3 
		   search_channel_5 
		   channel_active 
		   meetings_3plus
		   ageny_offer_10
		   vacancy_referrals
		   ageny_offer_13
		  ";
	  #d cr
	  
	  
	  use $data/data_pair, clear
	
	  bys pers_id: gen insample = 1 if _n==1
	  
	  
		*First stage
	    qui reg ausgaben_mp log_intensity_alt $cov county_pair_* [pw=1/fweight_reg] , cluster(region)
		matrix b=e(b)'
		matrix n=e(N)
		matrix v==e(V)
		matrix se=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		
		test log_intensity_alt
		scalar f=r(F)
		matrix d_fs2=b[1..1,1..1],se,f
		matrix d_fs1=.,.,.
		matrix d_fs=d_fs1,d_fs2
		
		
		foreach lhs in  $outc {
		*Reduced form
		qui reg `lhs' log_intensity_alt $cov  county_pair_*  [pw=1/fweight_reg], cluster(region)
		
		matrix b=e(b)'
		matrix n=e(N)
		matrix v==e(V)
		matrix se=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		
		sum `lhs' if e(sample)==1 & insample==1
		scalar m=r(mean)
		scalar r = 100*(b[1,1]/m)
		matrix d1_`lhs'=b[1..1,1..1],se,r
		
		
		
		
		*Second stage
		qui ivregress 2sls `lhs' $cov county_pair_* (ausgaben_mp = log_intensity_alt) [pw=1/fweight_reg], cluster(region)
	    matrix b=e(b)'
		matrix n=e(N)
		matrix v==e(V)
		matrix se=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		sum `lhs' if e(sample)==1 & insample==1
		scalar m=r(mean)
		scalar r = 100*(b[1,1]/m)
		matrix d2_`lhs'=b[1..1,1..1],se,r
		
		matrix d_`lhs' = d1_`lhs',d2_`lhs'
		}
		
		
		
		
		#delimit;
		matrix d=d_fs \
				 d_search_dist50 \
				 d_applications_total \
			     d_applications_distant \
				 d_applications_local \
				 d_log_distance_km \
				 d_search_channel_3 \
				 d_search_channel_5 \
				 d_channel_active \
				 d_meetings_3plus \
				 d_ageny_offer_10 \
				 d_vacancy_referrals \
				 d_ageny_offer_13 
	;
	
	matrix rownames d = 
	"First stage" 
	"Applied to distant" 
	"#applications total" 
	"#applications distant" 
	"#applications local" 
	"Search radius"  
	"Channel job info system"  
	"Channel caseworker"
	"Channel active" 
	"Meetings 3+" 
	"Any offer from agency" 
	"#offers from agency" 
	"Placement voucher" 
	;
	#delimit cr

	matrix colname d = "Reduced form" "SE" "%-effect" "2SLS" "SE" "%-effect/f-test"
	mat list d
	estout matrix(d, fmt(4)) using $data/Table_3_survey.tex, style(tex) replace
	

     
